package com.hk.photo;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hk.action.Action;
import com.hk.action.ActionForward;
import com.hk.dtos.AdminDto;
import com.hk.dtos.BBSDto;
import com.hk.dtos.PhotoDto;
import com.hk.dtos.ReplyDto;
import com.hk.dtos.SearchDto;
import com.hk.file.FileManager;
import com.hk.service.BBSService;
import com.hk.service.LoginService;
import com.hk.service.PhotoManager;
import com.hk.service.PhotoService;
import com.hk.service.ReplyService;
import com.jungbo.file.dto.FileDto;

public class PhotoListAction implements Action{
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		ActionForward forword=new ActionForward();
		HttpSession session = request.getSession();

		if(session.getAttribute("uid")==null){
			forword.setUrl("auterror");
			return forword;
		}
////////////////////////////////////////////
		LoginService ls= LoginService.getInstance();
			
		ArrayList<AdminDto> ad = ls.getmenu();
		
		AdminDto tad1 = new AdminDto();
		AdminDto tad2 = new AdminDto();
		AdminDto tad3 = new AdminDto();
		AdminDto tad4 = new AdminDto();
		AdminDto tad5 = new AdminDto();
		
		AdminDto tad6 = new AdminDto();
		AdminDto tad7 = new AdminDto();
		AdminDto tad8 = new AdminDto();
		AdminDto tad9 = new AdminDto();
		AdminDto tad10 = new AdminDto();
		AdminDto tad11 = new AdminDto();
		
		tad1=ad.get(0);
		tad2=ad.get(1);
		tad3=ad.get(2);
		tad4=ad.get(3);
		tad5=ad.get(4);
		
		tad6=ad.get(5);
		tad7=ad.get(6);
		tad8=ad.get(7);
		tad9=ad.get(8);
		tad10=ad.get(9);
		tad11=ad.get(10);
				
		request.setAttribute("menu1",tad1);
		request.setAttribute("menu2",tad2);
		request.setAttribute("menu3",tad3);
		request.setAttribute("menu4",tad4);
		request.setAttribute("menu5",tad5);
		
		request.setAttribute("leftmenu1",tad6);
		request.setAttribute("leftmenu2",tad7);
		request.setAttribute("leftmenu3",tad8);
		request.setAttribute("leftmenu4",tad9);
		request.setAttribute("leftmenu5",tad10);
		request.setAttribute("leftmenu6",tad11);
		////////////////////////////////////////////		
		PhotoService manager= PhotoService.getInstance();
		FileManager fmanager=new FileManager();
		ReplyService rmanager = ReplyService.getInstance();
		
		String shid = request.getParameter("seid");
		String shtitle = request.getParameter("setitle");
		ArrayList<ReplyDto> rdto = new ArrayList<ReplyDto>();
				
		rdto=rmanager.replyCount(2);
		
		List<FileDto> fdto = fmanager.getAllFileList();
		
		String date =manager.getDate();
		System.out.println(date);
		String spagenum = request.getParameter("pagenum");
		
		int pagenum=1;
		if(spagenum!=null){
		pagenum =Integer.parseInt(spagenum); 
		}
		
		int cpp;
		int ssp;
		if(pagenum>1){
			cpp=15*pagenum;
		
			ssp=1+cpp-15;
		}else{
			
			cpp=15;
			ssp=1;
		}
				
		int count = manager.pcountbbs();
		if(shid==null && shtitle==null){
			SearchDto sdto = new SearchDto();

			sdto.setCountPerPage(cpp);
			sdto.setStartPage(ssp);
			
			ArrayList<PhotoDto> dto= manager.pagebbs(sdto);
//			System.out.println(dto.get(0).getRegdate());
			request.setAttribute("date", date);
			request.setAttribute("rboards", rdto);
			request.setAttribute("pboards", dto);
			request.setAttribute("filelist", fdto);
			request.setAttribute("count",count);
			forword.setUrl("plist");
			forword.setRedirect(false);
			return forword;

		}else if(shid!=null && shtitle.equals("null")){

			SearchDto sdto = new SearchDto();

			sdto.setCountPerPage(cpp);
			sdto.setStartPage(ssp);
			sdto.setId(shid);
			
			ArrayList<PhotoDto> dto= manager.seidbbs(sdto);
			request.setAttribute("date", date);
			request.setAttribute("rboards", rdto);
			request.setAttribute("pboards", dto);
			request.setAttribute("filelist", fdto);
			request.setAttribute("count",count);

			forword.setUrl("plist");
			forword.setRedirect(false);
			return forword;
			
		}else if(shtitle!=null && shid.equals("null")){
			SearchDto sdto = new SearchDto();

			sdto.setCountPerPage(cpp);
			sdto.setStartPage(ssp);
			sdto.setTitle(shtitle);
			
			ArrayList<PhotoDto> dto= manager.setitlebbs(sdto);
			request.setAttribute("date", date);
			request.setAttribute("rboards", rdto);
			request.setAttribute("pboards", dto);
			request.setAttribute("filelist", fdto);
			request.setAttribute("count",count);

			forword.setUrl("plist");
			forword.setRedirect(false);
			return forword;


		}		
//		int temp = Integer.parseInt(date);
//		System.out.println("temp : ");
//		System.out.println(temp);
			
//		System.out.println(shid);System.out.println(shtitle);
		

		
			forword.setUrl("plist");
			forword.setRedirect(false);

		return forword;
	}//
}
